<?php
App::uses('AppController', 'Controller');
/**
 * Theathers Controller
 *
 * @property Theather $Theather
 */
class TheathersController extends AppController {
	public function beforeFilter() {
		$this->Security->validatePost = false;
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Theather->recursive = 0;
		$this->set('theathers', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Theather->id = $id;
		if (!$this->Theather->exists()) {
			throw new NotFoundException(__('Invalid theather'));
		}
		$theather =  $this->Theather->read(null, $id);
		$arr = array();
		foreach($theather['Seat'] as $seat ){
			$arr[$seat['row']][$seat['column']] = $seat;
		}
		
		$maxColumn = 0;
		$this->Theather->getMaxRow();
		$this->set('maxColumn', $maxColumn);
		$this->set('seats', $arr);
		$this->set('theather', $theather);
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Theather->create();
			if ($this->Theather->save($this->request->data)) {
				$this->Session->setFlash(__('The theather has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The theather could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Theather->id = $id;
		if (!$this->Theather->exists()) {
			throw new NotFoundException(__('Invalid theather'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Theather->save($this->request->data)) {
				$this->Session->setFlash(__('The theather has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The theather could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Theather->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Theather->id = $id;
		if (!$this->Theather->exists()) {
			throw new NotFoundException(__('Invalid theather'));
		}
		if ($this->Theather->delete()) {
			$this->Session->setFlash(__('Theather deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Theather was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
/**
 * createSeats 
 * @return void
 */
	public function createSeats(){
		Controller::loadModel('Seat');
		$rows = $this->request->data['rows'];
		$columns = $this->request->data['columns'];
		for($i=1; $i<=$rows; $i++){
			for($j=1; $j<=$columns; $j++){
				$this->Seat->create();
				if ($this->Seat->save(array('row' => $i, 'column' => $j, 'status' => 1, 'seat_type_id' => 1))) {
					$this->Session->setFlash(__('The seat has been saved'));
				} else {
					$this->Session->setFlash(__('The seat could not be saved. Please, try again.'));
				}
			}
		}
		$this->redirect(array('action' => 'view', $this->request->data['id']));
	}

}
